package org.reyton.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.reyton.entity.SysEmployee;
import org.reyton.service.SysEmployeeService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 员工表的控制器
 * Created by reyton on 16-11-30.
 */
@Controller("sysEmployeeAction")
@Scope("prototype")
public class SysEmployeeAction extends ActionSupport {

    private SysEmployee sysEmployee;
    private SysEmployeeService sysEmployeeService;

    @Resource(name = "sysEmployeeService")
    public void setSysEmployeeService(SysEmployeeService sysEmployeeService) {
        this.sysEmployeeService = sysEmployeeService;
    }


    /***
     * 登陆
     * @return 登陆成功或失败
     * @throws Exception
     */
    public String login() throws Exception {
        SysEmployee emp = sysEmployeeService.login(sysEmployee.getSn(), sysEmployee.getPassword());
        if (emp != null) {
            ServletActionContext.getRequest().getSession().setAttribute("EMP",emp);
            return "login_success";
        } else {
            return "login_faiure";
        }
    }

    /***
     *  注销
     * @return 注销成功
     * @throws Exception
     */
    public String logout() throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.invalidate();
        return "logout";
    }

    public SysEmployee getSysEmployee() {
        return sysEmployee;
    }

    public void setSysEmployee(SysEmployee sysEmployee) {
        this.sysEmployee = sysEmployee;
    }
}
